//
//  LineReplyTableCell.m
//  MiaoTu
//
//  Created by dllo on 16/9/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LineReplyTableCell.h"
#import "UIImageView+WebCache.h"
#import "UserInfo.h"
#import "MyDate.h"
#import "NSString+deleteZero.h"

@interface LineReplyTableCell ()
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *nickNameLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@end

@implementation LineReplyTableCell

- (void)setUserInfo:(UserInfo *)userInfo {
    _userInfo = userInfo;
    [_headImage sd_setImageWithURL:[NSURL URLWithString:_userInfo.HeadUrl]];
    [_headImage sizeToFit];
    _nickNameLabel.text = _userInfo.Nickname;
    [_nickNameLabel sizeToFit];
    
    MyDate *userDate = [MyDate myDateWithDateString:_userInfo.Created];
    MyDate *nowDate = [MyDate myDateWithDate:[NSDate date]];
    
    if ([userDate.certainDay isEqualToString:nowDate.certainDay]) {
        _timeLabel.text = [NSString stringWithFormat:@"%@ %@:%@", userDate.bigHour, userDate.hour, userDate.minute];
    } else if ([userDate.year isEqualToString:nowDate.year] && [userDate.month isEqualToString:nowDate.month] && userDate.day.integerValue + 1 == nowDate.day.integerValue) {
        _timeLabel.text = [NSString stringWithFormat:@"昨天 %@:%@", userDate.hour, userDate.minute];
    } else {
        _timeLabel.text = [NSString stringWithFormat:@"%@月%@日 %@:%@", [userDate.month noZeroString], [userDate.day noZeroString], userDate.hour, userDate.minute];
    }
    _timeLabel.font = [UIFont systemFontOfSize:14.f];
    _timeLabel.textColor = [UIColor colorWithWhite:0.624 alpha:1.000];
    [_timeLabel sizeToFit];
    _contentLabel.text = _userInfo.Content;
    [_contentLabel sizeToFit];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initLayout];
    }
    return self;
}

- (void)initLayout {
    _rootLayout = [MyRelativeLayout new];
    _rootLayout.widthDime.equalTo(self.contentView.widthDime);
    _rootLayout.wrapContentHeight = YES;
    [self.contentView addSubview:_rootLayout];
    
    _headImage = [UIImageView new];
    _headImage.topPos.equalTo(_rootLayout.topPos).offset(10);
    _headImage.leftPos.equalTo(_rootLayout.leftPos).offset(10);
    _headImage.widthDime.equalTo(@36);
    _headImage.heightDime.equalTo(@36);
    _headImage.viewLayoutCompleteBlock = ^(MyBaseLayout *layout, UIView *sbv) {
        sbv.layer.cornerRadius = sbv.frame.size.width / 2;
        sbv.layer.masksToBounds = YES;
    };
    [_rootLayout addSubview:_headImage];
    
    _nickNameLabel = [UILabel new];
    _nickNameLabel.topPos.equalTo(_headImage.topPos);
    _nickNameLabel.leftPos.equalTo(_headImage.rightPos).offset(10);
    [_rootLayout addSubview:_nickNameLabel];
    
    _timeLabel = [UILabel new];
    _timeLabel.topPos.equalTo(_nickNameLabel.bottomPos).offset(10);
    _timeLabel.leftPos.equalTo(_nickNameLabel.leftPos);
    [_rootLayout addSubview:_timeLabel];
    
    _contentLabel = [UILabel new];
    _contentLabel.leftPos.equalTo(_timeLabel.leftPos);
    _contentLabel.topPos.equalTo(_timeLabel.bottomPos).offset(10);
    _contentLabel.rightPos.equalTo(_rootLayout.rightPos).offset(20);
    _contentLabel.bottomPos.equalTo(_rootLayout.bottomPos).offset(10);
    _contentLabel.numberOfLines = 0;
    _contentLabel.flexedHeight = YES;
    [_rootLayout addSubview:_contentLabel];
    
    UIView *grayView = [UIView new];
    grayView.leftPos.equalTo(_contentLabel.leftPos);
    grayView.rightPos.equalTo(_rootLayout.rightPos);
    grayView.topPos.equalTo(_contentLabel.bottomPos).offset(10);
    grayView.heightDime.equalTo(@1);
    [_rootLayout addSubview:grayView];
    grayView.backgroundColor = [UIColor colorWithRed:0.910 green:0.906 blue:0.914 alpha:1.000];
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
